var config = require("../../config");
var app = getApp();

Page({
  data: {
    NavBarStyleData: 0,  //是否显示导航栏样式或显示
    NavBarItem: 2,    //导航栏默认选项
    navBarItem1:false,
    navBarItem2: false,    
    CityDropDownItem: -1,   //城市导航栏下拉默认选项
    ShopDropDownItem: -1,   //门店导航栏下拉默认选项
    list1: false,
    list2: false,    
    CityData: '',    //城市列表字段    
    ShopData: '',    //门店数据字段
    ShopType: '',   //门店类别字段
    ShopListData:{    //拨号模板数据
      show:false,
      text:'',
      cancel:'cancel',
      callOut:'callOut'
    },
    showItem:false,
    selectedCity:null,
    selectedShop:null,
  },
  onLoad() {

    // 初始页面默认调用定位,所以没传第二个参数
    getShop(this);

    // 调用获取城市列表
    getCityList(this);

    // 获取门店类别
    getShopType(this);

  },

  onShareAppMessage() {

  },
  
  /*导航栏*/
  NavBar(e) {
    let NavBarItem = e.currentTarget.dataset.index, td = this.data;
    console.log("yes");
    this.setData({
      NavBarStyleData: 1,
      scrollDistance: 0      
    });
    
    if (NavBarItem == 0) {
      this.setData({
        navBarItem1: !td.navBarItem1,
         DropDown1: false,
         navBarItem2: false, 
         list1: !td.list1,
         list2: false, 
      });
      if (td.CityData == '') {
        // 调用获取城市列表
        // getCityList(this);
      }
    } else if (NavBarItem == 1) {
      this.setData({
        navBarItem2: !td.navBarItem2,
        // DropDown1: false, 
        navBarItem1: false,        
        list1: false,
        list2: !td.list2, 
      });
      if (td.ShopType == '') {
        console.log("yes");
        // 调用门店类别
        // getShopType(this);
      }
    }else{

      this.setData({
        CityDropDownItem: -1,
        ShopDropDownItem: -1,
        NavBarItem,
        navBarItem1: false,
        DropDown1: false,        
        navBarItem2: false,        
        DropDown2: false,
        list1: false,
        list2: false, 
      });

      getShop(this);
    }
  },

  /*导航栏下拉之城市*/
  CityDropDown(e) {
    let CityDropDownItem = e.currentTarget.dataset.index, _this = this, td = this.data;
    let ShopTypeId = td.ShopType.data[td.ShopDropDownItem] ? td.ShopType.data[td.ShopDropDownItem].id : '';
    _this.setData({
      CityDropDownItem,
      navBarItem2: false,
      DropDown2: false,
      NavBarItem:null,      
      DropDown1:true,
      list1: !td.list1,         
      navBarItem1: !td.navBarItem1,
      showItem: !td.showItem,
      selectedShop: td.CityData.data[CityDropDownItem].name 
    });
        getCityList(this);
    setTimeout(() => {
      _this.setData({
        NavBarStyleData: 0
      });
      getShop(_this, td.CityData.data[CityDropDownItem].name, ShopTypeId);

    }, 150);
  },

  /*导航栏下拉之门店*/
  ShopDropDown(e) {
    let ShopDropDownItem = e.currentTarget.dataset.index, _this = this, td = this.data;
    let CityName = td.CityData.data[td.CityDropDownItem] ? td.CityData.data[td.CityDropDownItem].name : '';
    
    _this.setData({
      navBarItem1: false,
      DropDown1: false,
      NavBarItem: null,     
      ShopDropDownItem,
      DropDown2: true,
      navBarItem2: !td.navBarItem2,
      list2:!td.list2,
      selectedCity: td.ShopType.data[ShopDropDownItem].type_name  
    });
    getShopType(_this);        
    setTimeout(() => {
      _this.setData({
        NavBarStyleData: 0
      });
      getShop(_this, CityName , td.ShopType.data[ShopDropDownItem].id);
      
    }, 150);
  },

  /*定位中的更新定位*/
  RefreshLocation() {
    getShop(this);
  },

  /**触发拨号提示**/
  CallUp(e) {
    let phone = e.currentTarget.dataset.phone,td = this.data;
    wx.reportAnalytics('shop_list2_mobile', {
      'nums':1
    });
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    });
  },
  info(e){
    console.log("yes");
   
    let infoIdex = e.currentTarget.dataset.index, _this = this;
    _this.setData({
      infoIdex
    });
    wx.reportAnalytics('shop_list2', {
      'shop': _this.data.ShopData.data[infoIdex].store_name
    });
    setTimeout(() => {
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];  //当前页面
      var prevPage = pages[pages.length - 2]; //上一个页面
      prevPage.onShow(_this.data.ShopData.data[infoIdex]);
      wx.navigateBack();
    }, 200);

    // 统计所点击量
    // app.aldstat.sendEvent('选择门店', {
    //   '门店': _this.data.ShopData.data[infoIdex].store_name
    // });
  
  },

  /*查看门店详情*/ 
  findShopDetails(e){
    let td = this.data, 
    ShopId = e.currentTarget.dataset.shopid,
    index = e.currentTarget.dataset.index;
    console.log(td.ShopData.data[index].store_name);
    wx.reportAnalytics('shop_list2_detail', {
      'nums': 1,
      'shop': td.ShopData.data[index].store_name
    });
    wx.navigateTo({
      url: '../Shop2Details/Shop2Details?ShopId=' + ShopId,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    });
  },
  
  /*预约维修*/ 
  Reservation(e){
    let td = this.data,index = e.currentTarget.dataset.index;
    wx.reportAnalytics('shop_list2_maintain', {
      'nums': 1,
      'shop': td.ShopData.data[index].store_name
    });
    wx.navigateBack({
      delta: 2
    });
  }
});


/*获取门店数据*/
function getShop(e, city, type) {

  wx.showLoading({
    title: '加载中...',
    mask: true
  });

  wx.getLocation({    //获取位置
    success: function (resLoaction) {
      wx.request({    //获取门店列表
        method: "POST",
        url: config.service.ShopSite,
        data: {
          uid: wx.getStorageSync("loginInfo").uid,
          logintoken: wx.getStorageSync("loginInfo").token,
          lng: resLoaction.longitude,
          lat: resLoaction.latitude,
          city: city ? city : '',
          type: type ? type : '',
          is_send:1
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success(res) {
          console.log(res);
          wx.hideLoading();
          if (res.data.code == 1) {
            e.setData({
              ShopData: res.data
            });
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "loading",
              duration: 2000,
              mask: true
            });
          }

        }, fail(res) {
          wx.showToast({
            title: '请检查GPS是否开启',
            icon: 'loading',
            mask: true
          });
          wx.openSetting();
        }
      });

    }
  });
}

/*获取城市列表*/
function getCityList(e) {
  wx.request({
    method: "POST",
    url: config.service.CityList,
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success(res) {
      if (res.data.code == 1) {
        // console.log("获取城市列表:", res);
        e.setData({
          CityData: res.data
        });
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "loading",
          duration: 2000,
          mask: true
        });
      }
    }
  });
}


/*获取门店类别*/
function getShopType(e) {
  wx.request({
    method: "POST",
    url: config.service.ShopType,
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success(res) {
      if (res.data.code == 1) {
        e.setData({
          ShopType: res.data
        });
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "loading",
          duration: 2000,
          mask: true
        });
      }
    }
  });
}